<template>
  <div class="carousel">
    <div
      class="carousel-inner"
      :style="{ transform: `translateX(-${currentSlide * 100}%)` }"
    >
      <div v-for="(slide, index) in slides" :key="index" class="carousel-slide">
        <img :src="slide.image" :alt="slide.title" />
        <div class="carousel-content">
          <h2>{{ slide.title }}</h2>
          <p>{{ slide.description }}</p>
          <router-link :to="slide.link" class="btn-shop">立即购买</router-link>
        </div>
      </div>
    </div>
    <button class="carousel-btn prev" @click="prevSlide">❮</button>
    <button class="carousel-btn next" @click="nextSlide">❯</button>
    <div class="carousel-dots">
      <span
        v-for="(_, index) in slides"
        :key="index"
        :class="{ active: currentSlide === index }"
        @click="goToSlide(index)"
      ></span>
    </div>
  </div>
</template>

<script>
export default {
  name: "CarouselComponent",
  data() {
    return {
      currentSlide: 0,
      timer: null,
      slides: [
        {
          image: require("@/assets/images/banner/photo-1496062031456-07b8f162a322.jpg"),
          title: "浪漫七夕特惠",
          description: "精选玫瑰花束，为爱传情",
          link: "/products?category=roses",
        },
        {
          image: require("@/assets/images/banner/photo-1526047932273-341f2a7631f9.jpg"),
          title: "新品上市",
          description: "精致永生花礼盒",
          link: "/products?category=preserved",
        },
        {
          image: require("@/assets/images/banner/photo-1519378058457-4c29a0a2efac.jpg"),
          title: "商务花艺",
          description: "高端商务花艺定制",
          link: "/products?category=business",
        },
      ],
    };
  },
  methods: {
    nextSlide() {
      this.currentSlide = (this.currentSlide + 1) % this.slides.length;
    },
    prevSlide() {
      this.currentSlide =
        (this.currentSlide - 1 + this.slides.length) % this.slides.length;
    },
    goToSlide(index) {
      this.currentSlide = index;
    },
    startTimer() {
      this.timer = setInterval(this.nextSlide, 5000);
    },
  },
  mounted() {
    this.startTimer();
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  },
};
</script>

<style scoped>
.carousel {
  position: relative;
  width: 100%;
  height: 500px;
  overflow: hidden;
}

.carousel-inner {
  display: flex;
  transition: transform 0.5s ease-in-out;
  height: 100%;
}

.carousel-slide {
  min-width: 100%;
  position: relative;
}

.carousel-slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.carousel-content {
  position: absolute;
  top: 50%;
  left: 10%;
  transform: translateY(-50%);
  color: white;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
  max-width: 500px;
}

.carousel-content h2 {
  font-size: 2.5rem;
  margin-bottom: 1rem;
  font-weight: bold;
}

.carousel-content p {
  font-size: 1.2rem;
  margin-bottom: 2rem;
  opacity: 0.9;
}

.btn-shop {
  display: inline-block;
  padding: 12px 30px;
  background: #ff6b6b;
  color: white;
  text-decoration: none;
  border-radius: 25px;
  transition: all 0.3s;
  font-size: 1.1rem;
  border: 2px solid transparent;
}

.btn-shop:hover {
  background: transparent;
  border-color: white;
  transform: translateY(-2px);
}

.carousel-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(255, 255, 255, 0.2);
  border: none;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  cursor: pointer;
  color: white;
  font-size: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
  backdrop-filter: blur(5px);
}

.carousel-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: translateY(-50%) scale(1.1);
}

.carousel-btn.prev {
  left: 20px;
}

.carousel-btn.next {
  right: 20px;
}

.carousel-dots {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
}

.carousel-dots span {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.5);
  cursor: pointer;
  transition: all 0.3s;
}

.carousel-dots span:hover {
  background: rgba(255, 255, 255, 0.8);
}

.carousel-dots span.active {
  background: white;
  transform: scale(1.2);
}

@media (max-width: 768px) {
  .carousel {
    height: 400px;
  }

  .carousel-content {
    left: 5%;
    right: 5%;
    text-align: center;
  }

  .carousel-content h2 {
    font-size: 2rem;
  }

  .carousel-content p {
    font-size: 1rem;
  }
}
</style> 